package com.neusoft.ysu.populationmanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.neusoft.ysu.populationmanagement.constant.Errors;
import com.neusoft.ysu.populationmanagement.constant.TabelDic;
import com.neusoft.ysu.populationmanagement.entity.*;
import com.neusoft.ysu.populationmanagement.mapper.VImpPopMapper;
import com.neusoft.ysu.populationmanagement.vo.VImpPop;
import com.neusoft.ysu.populationmanagement.service.IVImpPopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * VIEW 服务实现类
 * </p>
 *
 * @author lhn
 * @since 2025-06-13
 */
@Service
public class VImpPopServiceImpl implements IVImpPopService {
    @Autowired
    OperationLogServiceImpl operationLogService;
    @Autowired
    PopulationServiceImpl populationService;
    @Autowired
    ImpPopServiceImpl impPopService;
    @Autowired
    VImpPopMapper vImpPopMapper;

    @Override
    public CommonResult insertVImp(VImpPop vImpPop, OperationLog log) {
        vImpPop.setId(null);
        vImpPop.setIsDelete(TabelDic.IS_DELETE_NO);
        Population population = new Population(vImpPop);
        // 检查人口信息
        CommonResult checkResult = populationService.checkPop(population);
        if (checkResult.getCode() != 1) {
            // 检查失败，返回301号错误
            return new CommonResult<>(Errors.ERROR_IMP_INSERT, checkResult.getMsg(), null);
        }
        // 检查并保存日志记录信息
        int fillResult = operationLogService.fillOperationLog(log, vImpPop.getIdnum());
        if (fillResult == 0) {
            // 检查失败，返回301号错误
            return new CommonResult<>(Errors.ERROR_IMP_INSERT, "根据身份证号未找到办理人员", null);
        }
        populationService.updatePop(population);
        vImpPop.setId(population.getId());
        ImpPop impPop = new ImpPop(vImpPop);
        CommonResult imp_result = impPopService.insertImpPop(impPop);
        if (imp_result.getCode() == 0) {
            imp_result.setCode(Errors.ERROR_IMP_INSERT);
            return imp_result;
        } else
            return new CommonResult<>(1, "重点人口信息插入成功", 1);
    }

    @Override
    public CommonResult updateVImp(VImpPop vImpPop, OperationLog log) {
        vImpPop.setId(null);
        Population population = new Population(vImpPop);
        // 检查人口信息
        CommonResult checkResult = populationService.checkPop(population);
        if (checkResult.getCode() != 1) {
            // 检查失败，返回303号错误
            return new CommonResult<>(Errors.ERROR_IMP_UPDATE, checkResult.getMsg(), null);
        }
        // 检查并保存日志记录信息
        int fillResult = operationLogService.fillOperationLog(log, vImpPop.getIdnum());
        if (fillResult == 0) {
            // 检查失败，返回303号错误
            return new CommonResult<>(Errors.ERROR_IMP_UPDATE, "根据身份证号未找到办理人员", null);
        }
        populationService.updatePop(population);
        vImpPop.setId(population.getId());
        ImpPop impPop = new ImpPop(vImpPop);
        CommonResult result = impPopService.updateImpPop(impPop);
        if (result.getCode() == 0) {
            result.setCode(Errors.ERROR_IMP_UPDATE);
            return result;
        } else
            return new CommonResult<>(1, "重点人口信息变更成功", 1);
    }

    @Override
    public CommonResult deleteVImp(VImpPop vImpPop, OperationLog log) {
        vImpPop.setId(null);
        Population population = new Population(vImpPop);
        // 检查人口信息
        CommonResult checkResult = populationService.checkPop(population);
        if (checkResult.getCode() != 1) {
            // 检查失败，返回306号错误
            return new CommonResult<>(Errors.ERROR_IMP_DELETE, checkResult.getMsg(), null);
        }
        // 检查并保存日志记录信息
        int fillResult = operationLogService.fillOperationLog(log, vImpPop.getIdnum());
        if (fillResult == 0) {
            // 检查失败，返回306号错误
            return new CommonResult<>(Errors.ERROR_IMP_DELETE, "根据身份证号未找到办理人员", null);
        }
        // 逻辑删除人口类中的数据
        population.setIsDelete(TabelDic.IS_DELETE_YES);
        populationService.updatePop(population);

        vImpPop.setId(population.getId());
        ImpPop impPop = new ImpPop(vImpPop);
        CommonResult result = impPopService.deleteImp(impPop);
        if (result.getCode() == 0) {
            result.setCode(Errors.ERROR_IMP_DELETE);
            return result;
        } else
            return new CommonResult<>(1, result.getMsg(), 1);
    }

    @Override
    public CommonResult selectImpByIdNum(String idnum) {
        List<VImpPop> impPopList = vImpPopMapper.selectByIdNum(idnum);
        if (impPopList.size() > 1) {
            System.out.println("error:Vimp表有重复数据");
            return new CommonResult<>(Errors.ERROR_IMP_SELECT, "Vimp表有重复数据", null);
        }
        if (impPopList.size() == 1) {
            return new CommonResult<>(1, "查询成功", impPopList.get(0));
        } else if (impPopList.size() == 0) {
            return new CommonResult<>(Errors.ERROR_IMP_SELECT, "未找到对应人员", null);
        }
        return new CommonResult<>(Errors.ERROR_IMP_SELECT, "不该这样呀", null);
    }

    /**
     * @param vImpPop
     * @param log
     * @return com.neusoft.ysu.populationmanagement.entity.CommonResult
     * @author ecl
     * @描述 公安干警提交变更申请
     * @date 2025-06-17
     */
    @Override
    public CommonResult sendApplication(VImpPop vImpPop, OperationLog log) {
        vImpPop.setId(null);
        Population population = new Population(vImpPop);
        // 检查人口信息
        CommonResult checkResult = populationService.checkPop(population);
        if (checkResult.getCode() != 1) {
            // 检查失败，返回304号错误
            return new CommonResult<>(Errors.ERROR_IMP_DIRECTOR_CHECK, checkResult.getMsg(), null);
        }
        // 检查并保存日志记录信息
        int fillResult = operationLogService.fillOperationLog(log, vImpPop.getIdnum());
        if (fillResult == 0) {
            // 检查失败，返回304号错误
            return new CommonResult<>(Errors.ERROR_IMP_DIRECTOR_CHECK, "根据身份证号未找到办理人员", null);
        }
        populationService.updatePop(population);
        vImpPop.setId(population.getId());
        ImpPop impPop = new ImpPop(vImpPop);
        CommonResult result = impPopService.insertUncheckImpPop(impPop);
        if (result.getCode() == 0) {
            result.setCode(Errors.ERROR_IMP_DIRECTOR_CHECK);
            return result;
        } else
            return new CommonResult<>(1, result.getMsg(), 1);
    }

    /**
     * @param vImpPop
     * @param log
     * @param is_pass 0：拒绝 1：通过
     * @return com.neusoft.ysu.populationmanagement.entity.CommonResult
     * @author ecl
     * @描述 公安局局长审核重点人口
     * @date 2025-06-17
     */
    @Override
    public CommonResult checkApplication(VImpPop vImpPop, OperationLog log, int is_pass) {
        vImpPop.setId(null);
        Population population = new Population(vImpPop);
        // 检查人口信息
        CommonResult checkResult = populationService.checkPop(population);
        if (checkResult.getCode() != 1) {
            // 检查失败，返回305号错误
            return new CommonResult<>(Errors.ERROR_IMP_DIRECTOR_CHECK, checkResult.getMsg(), null);
        }
        // 检查并保存日志记录信息
        int fillResult = operationLogService.fillOperationLog(log, vImpPop.getIdnum());
        if (fillResult == 0) {
            // 检查失败，返回304号错误
            return new CommonResult<>(Errors.ERROR_IMP_DIRECTOR_CHECK, "根据身份证号未找到办理人员", null);
        }
//        populationService.selectByIdNum(population.getIdnum());

        vImpPop.setId(((Population)(checkResult.getResult())).getId());
        ImpPop impPop = new ImpPop(vImpPop);
        CommonResult result = new CommonResult(0, "is_pass不为0或1", null);
        if (is_pass == 0) {
            // 审核不通过 设置为1，表示删除
            result = impPopService.finishCheckImpPop(impPop, TabelDic.IS_DELETE_YES);
        } else if (is_pass == 1) {
            result = impPopService.finishCheckImpPop(impPop, TabelDic.DELETE_DIRECTOR_CHECK);
        }
        if (result.getCode() == 0) {
            result.setCode(Errors.ERROR_IMP_DIRECTOR_CHECK);
            return result;
        } else
            return new CommonResult<>(1, result.getMsg(), 1);
    }

    /**
     * 公安局局长审核
     *
     * @param vImpPop
     * @param log
     * @param is_pass
     * @return com.neusoft.ysu.populationmanagement.entity.CommonResult
     * @author ecl
     * @date 2025-06-19
     */
    @Override
    public CommonResult finalApplication(VImpPop vImpPop, OperationLog log, int is_pass) {
        vImpPop.setId(null);
        Population population = new Population(vImpPop);
        // 检查人口信息
        CommonResult checkResult = populationService.checkPop(population);
        if (checkResult.getCode() != 1) {
            // 检查失败，返回305号错误
            return new CommonResult<>(Errors.ERROR_IMP_COMMISSIONER_CHECK, checkResult.getMsg(), null);
        }
        // 检查并保存日志记录信息
        int fillResult = operationLogService.fillOperationLog(log, vImpPop.getIdnum());
        if (fillResult == 0) {
            // 检查失败，返回305号错误
            return new CommonResult<>(Errors.ERROR_IMP_COMMISSIONER_CHECK, "根据身份证号未找到办理人员", null);
        }
//        populationService.updatePop(population);

        vImpPop.setId(((Population)(checkResult.getResult())).getId());
        ImpPop impPop = new ImpPop(vImpPop);
        CommonResult result = new CommonResult(0, "is_pass不为0或1", null);
        if (is_pass == 0) {
            // 审核不通过 设置为1，表示删除
            result = impPopService.finishFinalCheckImpPop(impPop, TabelDic.DELETE_COMMISIONER_CHECK_FAIL);
        } else if (is_pass == 1) {
            result = impPopService.finishFinalCheckImpPop(impPop, TabelDic.DELETE_COMMISIONER_CHECK_PASS);
        }
        if (result.getCode() == 0) {
            result.setCode(Errors.ERROR_IMP_COMMISSIONER_CHECK);
            return result;
        } else
            return new CommonResult<>(1, result.getMsg(), 1);
    }

    @Override
    public CommonResult selectChose(Integer option) {
        LambdaQueryWrapper<VImpPop> lqw = new LambdaQueryWrapper();
        lqw.eq(VImpPop::getIsDelete, option);
        return new CommonResult<>(1,"成功",vImpPopMapper.selectList(lqw));

    }
}
